package so.wy.servicecenter.businessbed.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import so.wy.servicecenter.base.exception.ServiceException;
import so.wy.servicecenter.base.exception.WebException;
import so.wy.servicecenter.base.frame.web.AbstractWeb;
import so.wy.servicecenter.base.frame.web.webhand.WebExceptionHandler;
import so.wy.servicecenter.businessbed.manager.normal.wx.WxDecodeDTO;
import so.wy.servicecenter.businessbed.manager.normal.wx.WxSessionDTO;
import so.wy.servicecenter.businessbed.service.rentuser.dto.RentUserDTO;
import so.wy.servicecenter.businessbed.service.rentuser.dto.RentUserDetailsDTO;
import so.wy.servicecenter.businessbed.service.user.TUserAppService;

@Service
@Scope("prototype")
public class TUserAppWeb extends AbstractWeb {
    @Autowired
    private TUserAppService userTAppService;

    //微信登陆或注册
    @WebExceptionHandler
    public String userWxLoginOrRegister(String ip, String openId, String phone) throws WebException {
        try {
            return userTAppService.userWxLoginOrRegister(ip, openId, phone);
        } catch (ServiceException e) {
            throw exception(e);
        }
    }

    /**
     * 更新用户
     */
    @WebExceptionHandler
    public void updateUser(long userId, String name, String icon) throws WebException {
        try {
             userTAppService.updateUser(userId, name, icon);
        } catch (ServiceException e) {
            throw exception(e);
        }
    }

    @WebExceptionHandler
    public RentUserDetailsDTO getRentUserInfo(Long userId){
        return userTAppService.getRentUserInfo(userId);
    }

    //获取微信session
    @WebExceptionHandler
    public WxSessionDTO getWxProCode2Session(String jsCode) throws WebException {
        try {
            return userTAppService.getWxProCode2Session(jsCode);
        } catch (ServiceException e) {
            throw exception(e);
        }
    }

    //解密用户信息
    @WebExceptionHandler
    public WxDecodeDTO decrypt(String encryptedData, String sessionKey, String iv) throws WebException {
        try {
            return userTAppService.decrypt(encryptedData, sessionKey, iv);
        } catch (ServiceException e) {
            throw exception(e);
        }
    }
}
